#include "userConfig.h"
#include "usart.h"
#include "task.h"
#include "mm.h"
#include "queue.h"
#include "mutex.h"

//任务优先级
#define START_TASK_PRIO		1
//任务堆栈大小
#define START_STK_SIZE 		128
//任务句柄

#define TASK_DELAY      100

struct task_struct * StartTask_Handler;
struct task_struct * Task_Handler7;
struct task_struct * Task_Handler8;
struct task_struct * Task_Handler9;
struct task_struct * Task_Handler10;

void task7_task(void *args);
void task8_task(void *args);
void task9_task(void *args);
void task10_task(void *args);

//任务优先级
#define TASK_TASK_LOW_PRIO		1
#define TASK_TASK_MID_PRIO		1
#define TASK_TASK_HIGH_PRIO     1
//任务堆栈大小
#define TASK_STK_SIZE 		512
//任务句柄
struct msg_queue * big_date_queue;   /* 大数据句柄 */
struct mutex * semphore_handle;   /* 大数据句柄 */


/* 任务一，实现入队 */
static void task1_task(void *pvParameters)
{
    uint32_t send = 0;
    uint32_t   err = 0;

    while(1)
    {
        err = queue_write(big_date_queue, &send, MAX_DELAY);
        if(err != 1)
        {
            usart_transmit("queue队列发送失败\r\n");
        } else {
            send++;
            if (send == 99999)
                send = 0;
            // usart_transmit("queue队列发送成功: %d\r\n", count++);
        }
        task_delay(5000);
    }
}

/* 任务二，小数据出队 */
static void task2_task(void *pvParameters)
{
    uint32_t recv_count = 0;
    uint32_t err = 0;

    while(1)
    {
        err = queue_read(big_date_queue, &recv_count, 91);
        if(err != 1)
        {
            usart_transmit("big_date_queue队列读取失败\r\n");
        }else
        {
            usart_transmit("数据：%u\n", recv_count);

        }
    }
}

void task3_task(void *args)
{
	while(1)
	{
        usart_transmit("high任务尝试获取信号量\r\n");
        task_delay(TASK_DELAY);
        down(semphore_handle, MAX_DELAY);
        usart_transmit("high任务成功获取信号量\r\n");
        usart_transmit("high任务释放信号量\n");
        up(semphore_handle);

        task_delay(TASK_DELAY);
	}
}

void task4_task(void *args)
{
	uint32_t task4_num=0;
    uint32_t times;

	while(1)
	{
        usart_transmit("low 任务尝试获取信号量\r\n");
        down(semphore_handle, MAX_DELAY);
        usart_transmit("low 任务成功获取信号量\r\n");
        usart_transmit("low 任务释放信号量\r\n");
        up(semphore_handle);

        task_delay(TASK_DELAY);
	}
}

void task5_task(void *args)
{
	uint32_t task5_num=0;

	while(1)
	{
		task5_num++;
		usart_transmit("mid 任务5已经执行:%d次\r\n",task5_num);
        task_delay(TASK_DELAY);
	}
}

void task6_task(void *args)
{
	uint32_t task6_num=0;
    char *p = NULL;

	while(1)
	{
		task6_num++;
		usart_transmit("任务6已经执行:%d次\r\n",task6_num);

        p = kmalloc(32);
        task_delay(TASK_DELAY);
        kfree(p);
	}
}

void task7_task(void *args)
{
	uint32_t task7_num=0;

    Task_Handler8 = task_create(task8_task,
                "task8_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_HIGH_PRIO);

    Task_Handler9 = task_create(task9_task,
                "task9_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_HIGH_PRIO);

    Task_Handler10 = task_create(task10_task,
                "task10_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_HIGH_PRIO);


    task_delete(Task_Handler7);
}

void task8_task(void *args)
{
	uint32_t task8_num=0;

	while(1)
	{
		task8_num++;
		usart_transmit("任务8已经执行:%d次\r\n",task8_num);
        task_delay(TASK_DELAY);
	}
}

void task9_task(void *args)
{
	uint32_t task9_num=0;

	while(1)
	{
		task9_num++;
		usart_transmit("任务9已经执行:%d次\r\n",task9_num);
        task_delay(TASK_DELAY);
	}
}

void task10_task(void *args)
{
	uint32_t task10_num=0;

	while(1)
	{
		task10_num++;
		usart_transmit("任务10已经执行:%d次\r\n",task10_num);
        task_delay(TASK_DELAY);
	}
}

//开始任务任务函数
void start_task(void *args)
{
    disable_interrupt();

    task_create(task1_task,
                "task1_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_LOW_PRIO);

    task_create(task2_task,
                "task2_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_HIGH_PRIO);

    // task_create(task3_task,
    //             "task3_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_HIGH_PRIO);

    // task_create(task4_task,
    //             "task4_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task5_task,
    //             "task5_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_MID_PRIO);

    // task_create(task6_task,
    //             "task6_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_HIGH_PRIO);

    Task_Handler7 = task_create(task7_task,
                "task7_task",
                (uint16_t       )TASK_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task9_task,
    //             "task9_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_LOW_PRIO);

    // task_create(task10_task,
    //             "task10_task",
    //             (uint16_t       )TASK_STK_SIZE,
    //             (void*          )NULL,
    //             (unsigned long    )TASK_TASK_HIGH_PRIO);

    task_delete(StartTask_Handler);
    enable_interrupt();
}

void task_demo(void)
{

    /* 队列的创建 */
    big_date_queue = queue_create(5, sizeof(uint32_t));
    if(big_date_queue != NULL)
    {
        usart_transmit("big_date_queue队列创建成功\r\n");
    }
    else
    {
        usart_transmit("big_date_queue队列创建失败\r\n");
    }

    semphore_handle = mutex_create();
    if(semphore_handle != NULL)
    {
        usart_transmit("信号量创建成功！！！\r\n");
    }
    up(semphore_handle);        /* 释放一次信号量 */

    //创建开始任务
    StartTask_Handler = task_create(start_task,
                "start_task",
                (uint16_t       )START_STK_SIZE,
                (void*          )NULL,
                (unsigned long    )START_TASK_PRIO);
}




